package com.fractalist.man.base.action;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fractalist.base.cache.Cache;
import com.fractalist.base.core.model.JsonSuccessResponse;
import com.fractalist.base.core.web.BaseActionSupport;

@Controller
@Scope("prototype")
@ParentPackage("default")
public class TestAction extends BaseActionSupport {

	private static final long serialVersionUID = -2111808911345170917L;

	private static final Logger logger = Logger.getLogger(TestAction.class);

	private Map<String, Object> paramMap;

	private String uploadFileURL;
	private String uploadFileSize;
	private String uploadFileName;

	/**
	 * 测试struts
	 */
	@Action(value = "/test", interceptorRefs = @InterceptorRef("withoutAuthStack"))
	public String test() {
		JsonSuccessResponse res = new JsonSuccessResponse();
		res.getData().put("type", "test");
		Cache.add("test", 1, "0s");
		Cache.add("test2", 1, "1h");
		List list1 = Cache.getKeys();
		List list2 = Cache.getKeysNoDuplicateCheck();
		// list3中去掉了失效的key“test”，这个注意下
		List list3 = Cache.getKeysWithExpiryCheck();
		return renderJson(res);
	}

	/**
	 * 测试struts
	 */
	@Action(value = "/baseUpload", interceptorRefs = @InterceptorRef("withUploadStack"))
	public String baseUpload() {
		JsonSuccessResponse res = new JsonSuccessResponse();
		Collection<String> errors = getActionErrors();
		if (errors != null && errors.size() > 0) {
			Iterator<String> it = errors.iterator();
			while(it.hasNext()){
				String key = it.next();
				System.out.println(key);
				res.getData().put("error", key);
			}
		} else {
			res.getData().put("uploadFileURL", uploadFileURL);
			res.getData().put("uploadFileSize", uploadFileSize);
			res.getData().put("uploadFileName", uploadFileName);
		}
		return renderJson(res);
	}

	public void setParamMap(Map<String, Object> paramMap) {
		this.paramMap = paramMap;
	}

	public void setUploadFileURL(String uploadFileURL) {
		this.uploadFileURL = uploadFileURL;
	}

	public void setUploadFileSize(String uploadFileSize) {
		this.uploadFileSize = uploadFileSize;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

}
